'use strict';

/**
 * 状态
 * @type {{DUSTBIN: string, NORMAL: string}}
 */
const stateKeys = {
  /**
   * 回收站状态
   */
  DUSTBIN: 'DUSTBIN',
  /**
   * 正常状态
   */
  NORMAL: 'NORMAL',
  /**
   * 删除状态
   */
  DELETE: 'DELETE',
};

module.exports = {
  BaseParameter: {
    /**
     * 状态字段
     * NORMAL: 正常状态
     * DUSTBIN:
     */
    stateKey: { type: String, default: stateKeys.NORMAL, enum: [ 'DUSTBIN', 'NORMAL', 'DELETE' ] },
  },
  /**
   * 状态数组
   */
  stateKeys,
  /**
   * 获取ID
   */
  addCommentSchema(Schema, mongoose) {
    /**
     * 添加评论
     */
    Schema.statics.addComments = async function(id, commentsId) {
      return this.updateOne({
        _id: mongoose.Types.ObjectId(id),
      }, {
        $push: {
          commentsList: mongoose.Types.ObjectId(commentsId),
        },
      });
    };

    // 删除评论
    Schema.statics.removeComments = function(id, commentsId){
      return this.updateOne({
        _id: mongoose.Types.ObjectId(id),
      }, {
        $pull: {
          commentsList: {
            _id: mongoose.Types.ObjectId(commentsId),
          }
        }
      })
    }
  },
  CommentQuery(Schema, mongoose)  {
    return {
      commentsList: [{ type: Schema.Types.ObjectId, ref: 'Comments' }],
    }
  }
};
